package common

import (
	"encoding/json"
	"testing"

	"github.com/go-playground/assert/v2"
	. "github.com/smartystreets/goconvey/convey"
)

func TestGetAppCsName(t *testing.T) {
	assert.Equal(t, getAppCsName("55133"), "app-55133")
	assert.Equal(t, getAppCsName("55133.123"), "app-55133-123")
	assert.Equal(t, getAppCsName("55133_1_23"), "cron-55133-1-23")
}

func TestJsonUnMashalBoolOrString(t *testing.T) {
	Convey("test json unmshal bool or string", t, func() {
		strBoolStr := `{"isDir": "true", "ossPath":"op", "localPath" : "lp"}`
		d := DownloadList{}
		err := json.Unmarshal([]byte(strBoolStr), &d)
		So(err, ShouldBeNil)
		So(d.IsDir, ShouldEqual, true)

		strBoolStr = `{"isDir": "false", "ossPath":"op", "localPath" : "lp"}`
		d = DownloadList{}
		err = json.Unmarshal([]byte(strBoolStr), &d)
		So(err, ShouldBeNil)
		So(d.IsDir, ShouldEqual, false)

		strBoolStr = `{"isDir": true, "ossPath":"op", "localPath" : "lp"}`
		d = DownloadList{}
		err = json.Unmarshal([]byte(strBoolStr), &d)
		So(err, ShouldBeNil)
		So(d.IsDir, ShouldEqual, true)

		strBoolStr = `{"isDir": true, "ossPath":"op", "localPath" : "lp"}`
		d = DownloadList{}
		err = json.Unmarshal([]byte(strBoolStr), &d)
		So(err, ShouldBeNil)
		So(d.IsDir, ShouldEqual, true)

		strBoolStr = `{"isDir": "asdf", "ossPath":"op", "localPath" : "lp"}`
		d = DownloadList{}
		err = json.Unmarshal([]byte(strBoolStr), &d)
		So(err, ShouldNotBeNil)
	})

}
